package com.pcbly.web.action.dict;

import java.util.ArrayList;
import java.util.List;

import com.pcbly.web.action.BaseAction;
import com.pcbly.web.bean.dict.DictCategoryAddChildBean;
import com.pcbly.web.bean.dict.DictCategoryBean;
import com.pcbly.web.business.dict.DictCategoryService; 
import com.pcbly.web.components.utils.random.GUID;

public class DictionaryAction extends BaseAction<DictCategoryBean> {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2368374180871432991L;

	private DictCategoryService dictCategoryService;
	private List<DictCategoryAddChildBean> dictCategoryAddChildBeanList;

	private DictCategoryBean dictCategoryBean;
	
	private String ids;
	
	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public DictCategoryBean getDictCategoryBean() {
		return dictCategoryBean;
	}

	public void setDictCategoryBean(DictCategoryBean dictCategoryBean) {
		this.dictCategoryBean = dictCategoryBean;
	}

	public String index() throws Exception {
		long startTime = System.currentTimeMillis();
		dictCategoryAddChildBeanList = dictCategoryService
				.queryAllDictCategoryAddChild();
		if (log.isDebugEnabled()) {
			log.debug("login index..........");
		}
//		int page = getPage();

//		Page<DictCategoryAddChildBean> pageList = new Page<DictCategoryAddChildBean>(getPageSize());
//		pageList.setResult(dictCategoryAddChildBeanList);
//		pageList.setPageNo(page);
		startTime = System.currentTimeMillis() - startTime;
		if (log.isDebugEnabled()) {
			log.debug("time ms:::::" + startTime);
			System.out.println("time ms:::::" + startTime);
		}
		return super.index();
	}
	
	public String dwrDelete(String id) throws Exception {
		if (log.isDebugEnabled()) {
			log.debug("login delete..........");
			log.debug(id);
		}
		dictCategoryService.removeDictCategoryById(id);
		return super.dwrBaseDelete(id);
	}

	public List<DictCategoryBean> mainCategory(String id) throws Exception {
		if (log.isDebugEnabled()) {
			log.debug("login mainCategory..........");
			log.debug(id);
		}
		List<DictCategoryBean> beans = new ArrayList<DictCategoryBean>();
		List<DictCategoryBean> tbean = dictCategoryService
				.queryAllDictCategory();
		for (DictCategoryBean bean : tbean) {
			if (!bean.getId().equals(id)) {
				beans.add(bean);
			}
		}
		return beans;
	}

	public List<DictCategoryBean> queryDictCategoryBeanByMasterId(
			String masterId) throws Exception {
		List<DictCategoryBean> list = new ArrayList<DictCategoryBean>();
		DictCategoryBean bean = new DictCategoryBean();
		bean.setId("-1");
		bean.setName(" ");
		list.add(bean);
		list.addAll(dictCategoryService
				.queryDictCategoryBeanByMasterId(masterId));
		return list;
	}

	/**
	 * 显示顶层菜单详细信息
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public DictCategoryBean treeDetail(String id) throws Exception {
		if (log.isDebugEnabled()) {
			log.debug("login treeDetail..........");
			log.debug(id);
		}
		// dictCategoryBean=dictCategoryService.findById(id);
		return dictCategoryService.findById(id);
	}

	/**
	 * 保存顶层菜单
	 * 
	 * @param bean
	 * @return
	 * @throws Exception
	 */
	public String dwrSave(DictCategoryBean bean) throws Exception {
		if (log.isDebugEnabled()) {
			log.debug("login save..........");
			log.debug(bean.getId());
		}
		try{
		if (bean.getId() == null || "".equals(bean.getId())) {// new
			GUID guid = new GUID();
			bean.setId(guid.toString());
			dictCategoryService.createDictCategoryBean(bean);
		} else {// update
			dictCategoryService.updateDictCategoryBean(bean);
		}}catch(Exception e){
			e.printStackTrace();
		}
		return super.dwrBaseSave(bean.getId());
	}

	public void setDictCategoryService(DictCategoryService dictCategoryService) {
		this.dictCategoryService = dictCategoryService;
	}

	public void setDictCategoryAddChildBeanList(
			List<DictCategoryAddChildBean> dictCategoryAddChildBeanList) {
		this.dictCategoryAddChildBeanList = dictCategoryAddChildBeanList;
	}

	public List<DictCategoryAddChildBean> getDictCategoryAddChildBeanList() {
		return dictCategoryAddChildBeanList;
	}

}
